package com.gmstu.defaults.aspect;

import com.gmstu.springframework.aop.aspect.GpJoinPoint;
import com.gmstu.springframework.aop.intercept.GpMethodInvocation;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * @author John.zhang
 * @version Id: LogAspect, v 1.0 2020/8/26 20:40 ZhiYong Exp $
 */
@Slf4j
public class LogAspect {

    public void before(GpJoinPoint joinPoint) {

        try {
            ((GpMethodInvocation)joinPoint).proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        joinPoint.setUserAttribute("startTime_" + joinPoint.getMethod().getName(), System.currentTimeMillis());
        log.info("Invoker Before Method!!!" +
                "\nTargetObject:" + joinPoint.getThis() +
                "\nArgs:" + Arrays.toString(joinPoint.getArguments()));
    }

    public void after(GpJoinPoint joinPoint) {
        log.info("Invoker After Method!!!" +
                "\nTargetObject:" + joinPoint.getThis() +
                "\nArgs:" + Arrays.toString(joinPoint.getArguments()));
        long startTime = (Long) joinPoint.getUserAttribute("startTime_" + joinPoint.getMethod().getName());
        long endTime = System.currentTimeMillis();
        System.out.println("use time :" + (endTime - startTime));
    }

    public void afterThrowing(GpJoinPoint joinPoint, Throwable throwable) {
        log.info("出现异常" +
                "\nTargetObject:" + joinPoint.getThis() +
                "\nArgs:" + Arrays.toString(joinPoint.getArguments()) +
                "\nThrows:" + throwable.getMessage());
    }


}
